<?php

namespace Have\Controller;

class MapController extends HaveController {

    protected $_obds = array();

    protected function _initialize(){
        parent::_initialize();
        if (USER_AUTH > 1) {
            $this->redirect('Index/index');
        }
        $this->_obds = $this->getAllDev();
        $this->assign('obdid', $this->_obds);
    }
    public function index() {
        $this->meta_title = '地图';
        $this->display();
    }

    public function showall($obdid=0) {
        $this->meta_title = '地图';
        $this->display();
    }
    
    final private function transform($coord = array()) {
        $pi = M_PI;
        $xpi = M_PI * 3000.0 / 180.0;
        $a = 6378245.0;
        $ee = 0.00669342162296594323;
        foreach ($coord as $key => $item) {
            if (doubleval($coord[$key]['longitude']) > 137.8347 || doubleval($coord[$key]['longitude']) < 72.004)
                continue;
            if (doubleval($coord[$key]['latitude']) > 55.8271 || doubleval($coord[$key]['latitude']) < 0.8293)
                continue;
            $wglat = doubleval($item['latitude']);
            $wglon = doubleval($item['longitude']);
            $x = $wglon - 105.0;
            $y = $wglat - 35.0;
            $dlat = -100.0 + 2.0 * $x + 3.0 * $y + 0.2 * $y * $y + 0.1 * $x * $y + 0.2 * sqrt(abs($x));
            $dlat += (20.0 * sin(6.0 * $x * $pi) + 20.0 * sin(2.0 * $x * $pi)) * 2.0 / 3.0;
            $dlat += (20.0 * sin($y * $pi) + 40.0 * sin($y / 3.0 * $pi)) * 2.0 / 3.0;
            $dlat += (160.0 * sin($y / 12.0 * $pi) + 320 * sin($y * $pi / 30.0)) * 2.0 / 3.0;
            $dlon = 300.0 + $x + 2.0 * $y + 0.1 * $x * $x + 0.1 * $x * $y + 0.1 * sqrt(abs($x));
            $dlon += (20.0 * sin(6.0 * $x * $pi) + 20.0 * sin(2.0 * $x * $pi)) * 2.0 / 3.0;
            $dlon += (20.0 * sin($x * $pi) + 40.0 * sin($x / 3.0 * $pi)) * 2.0 / 3.0;
            $dlon += (150.0 * sin($x / 12.0 * $pi) + 300.0 * sin($x / 30.0 * $pi)) * 2.0 / 3.0;
            $redlat = $wglat / 180.0 * $pi;
            $magic = sin($redlat);
            $magic = 1 - $ee * $magic * $magic;
            $sqrtMagic = sqrt($magic);
            $dlat = ($dlat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * $pi);
            $dlon= ($dlon * 180.0) / ($a / $sqrtMagic * cos($redlat) * $pi);
            $wglat += $dlat;
            $wglon += $dlon;
            $x = $wglon;
            $y = $wglat;
            $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $xpi);
            $theza = atan2($y, $x) + 0.000003 * cos($x * $xpi);
            $coord[$key]['latitude'] = $z * sin($theza) + 0.006;
            $coord[$key]['longitude'] = $z *cos($theza) + 0.0065;
        }
        return $coord;
    }

    public function line(){
        if (empty($_POST)) {
            echo '';
            return;
        }
        $line = $this->getLine($_POST);
        if (!empty($line)) {
            $line = $this->transform($line);
            $str = '';
            foreach ($line as $item) {
                $str .= $item['longitude'].','.$item['latitude'].';';
            }
            $str[strlen($str)-1] = 0;
            echo $str;
        }else{
            echo '';
        }
    }
    
    public function getLocate($obdid=''){
        $locate = D('Allobd')->getLastlocate($obdid);
        foreach ($locate as $value) {
            $locates .= $value['obdid'].','.$value['lng'].','.$value['lat'].';';
        }
        $locates[strlen($locates)-1] = 0;
        echo $locates;
    }

    protected function getLine($obdid='') {
        if ('' === $obdid) {
            return null;
        }
        try {
           $date = '';
           if (is_array($obdid)) {
                $where['obdid'] = $_POST['obdid'];
                $where['time'] = array(array('egt', $_POST['start']), array('elt', $_POST['stop']));
                $date = substr(str_replace('-', '', $_POST['start']), 0, 8);
           } else {
                $date = $this->getDate('Ymd');
                $where['obdid'] = $obdid;
           }
           $line = M('gpsdata_'.$date);
           return $line->where($where)->field('longitude,latitude')->order('time asc')->select();
        }catch(\Exception $ex) {
            return null;
        }
    }
}

?>
